use once_cell::sync::Lazy;
use rbatis::rbatis::RBatis;
use rbdc_mysql::driver;

use super::config;

pub static MYSQL: Lazy<RBatis> = Lazy::new(RBatis::new);

pub async fn init_pool() {
    let database_url = config::CONFIG.mysql();
    log::info!("rbatis pool init ({})...", database_url);
    let driver = driver::MysqlDriver {};
    let driver_name = format!("{:?}", driver);
    MYSQL
        .init(driver, &database_url)
        .expect("rbatis pool init fail!");
    MYSQL.acquire().await.unwrap_or_else(|_| {
        panic!(
            "rbatis connect database(dirver={},url={}) fail",
            driver_name, database_url
        )
    });
    log::info!(
        "rbatis pool init success! pool state = {:#?}",
        MYSQL.get_pool().expect("pool not init!").state().await
    );
}
